package io.github.i678.bukkitantipmc.listeners;

import io.github.i678.bukkitantipmc.events.AntiPMCFiltered;
import org.bukkit.BanList;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;

import static io.github.i678.bukkitantipmc.utils.ConfigUtils.getBoolean;
import static io.github.i678.bukkitantipmc.utils.FilterUtils.isNormal;
import static io.github.i678.bukkitantipmc.utils.FilterUtils.isSevere;

/**
 * Was called "ListenerFile" before renaming to this.
 * @author TheNameMan
 * @version 2.0.0
 */
public class ChatFilter implements Listener {

    @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
    public void chatCheck(AsyncPlayerChatEvent ev) {
        String message = ev.getMessage().toLowerCase();
        AntiPMCFiltered e = null;
        if(isSevere(message) && getBoolean("filter.severeFilter")) {
            e = new AntiPMCFiltered(ev.getMessage(), ev.getPlayer(), ev.getPlayer().hasPermission("antifakepmc.bypass"), true, getBoolean("filter.banOnSevere"));
            Bukkit.getPluginManager().callEvent(e);
            ev.setCancelled(true);
            if(getBoolean("filter.banOnSevere")) {
                Bukkit.getBanList(BanList.Type.NAME).addBan(ev.getPlayer().getName(), ChatColor.RED + "Filtered ", null, "AntiFakePMC");
            }
        } else if(isNormal(message)) {
            ev.setCancelled(true);
        }
        if(e == null) return;
        e = null;
    }

}